package icu.xblogs.report.controller;

import icu.xblogs.report.domain.Employee;
import icu.xblogs.report.domain.Manager;
import icu.xblogs.report.service.EmployeeService;
import icu.xblogs.report.service.ManagerService;
import icu.xblogs.report.utils.CookieUtil;
import icu.xblogs.report.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author Frank KONG
 * @version 1.0
 * @date 2020/11/17 10:21
 *
 * 该Controller下的请求不被拦截
 * status : 0-管理员; 1-员工
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    private EmployeeService employeeService;
    private ManagerService managerService;

    @Autowired
    @Qualifier("employeeService")
    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @Autowired
    @Qualifier("managerService")
    public void setManagerService(ManagerService managerService) {
        this.managerService = managerService;
    }

    @RequestMapping("/test")
    public void test(){
        System.out.println("manager test...");
    }

    @RequestMapping(value = "/employeeLogin", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String employeeLogin(int id, String password, HttpServletResponse response){
        System.out.println("123");
        Employee employee = employeeService.getByIdAndPsw(id, password);
        if(employee == null || employee.getId() == 0) return "fail";
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("status", "1");                             //身份
        hashMap.put("id", String.valueOf(employee.getId()));    //id
        hashMap.put("token", JwtUtil.getJWT(String.valueOf(employee.getId()))); //token
        System.out.println(hashMap.toString());
        CookieUtil.addCookie(response, hashMap);
        return "success";
    }

    @PostMapping("/managerLogin")
    @ResponseBody
    public String managerLogin(int id, String password, HttpServletResponse response){
        Manager manager = managerService.getByIdAndPsw(id, password);
        if(manager == null || manager.getId() == 0) return "fail";
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("status", "0");                             //身份
        hashMap.put("id", String.valueOf(manager.getId()));     //id
        hashMap.put("token", JwtUtil.getJWT(String.valueOf(manager.getId()))); //token
        CookieUtil.addCookie(response, hashMap);
        return "success";
    }

    @RequestMapping("/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CookieUtil.removeCookie(request, response, "id", "status", "token");
        response.sendRedirect("../");
    }

}
